package com.hc.model.code02.test11;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @version v1.0
 * @ClassName: CoffeeFactory
 * @Description: 咖啡工厂类
 * @Author: HC
 */
public class CoffeeFactory {

    private final static Map<String, Coffee> map = new HashMap<>();

    static {
        try {
            Properties pro = new Properties();
            InputStream is = CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            pro.load(is);
            Set<Object> keySet = pro.keySet();
            for (Object key : keySet) {
                String value = pro.getProperty((String) key);
                // 加载对象
                Class<?> cls = Class.forName(value);
                Coffee c = (Coffee) cls.newInstance();
                map.put((String) key, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Coffee createCoffee(String name) {
        return map.get(name);
    }
}
